define(['text!/template/${moduleMapperCode}/${(businessCode?uncap_first)!''}/index.tpl.html'], function (indexTpl) {
    'use strict';
    return Marionette.LayoutView.extend({
        id: _.uniqueId("${moduleMapperCode}-${(businessCode?uncap_first)!''}-index-"),
        className: "pure-g",
        initialize: function () {
            this.template = _.template(indexTpl);
        },
        serializeData: function () {
            var data = this.options.data;

            return data;
        },
        onRender: function () {
            _.delay(function (that) {
                that.$('div[data-id="table-1"]').table({
                    $el: that.$el,
                    data: that.options.data
                });
            }, 500, this);
        },
        events: {
            'click a[data-id="add"]': "onAdd",
            'click a[data-id="modify"]': "onModify",
            'click a[data-id="delete"]': "onDelete",
            'dblclick [data-id="table-1"] tr[data-id]': "onSelectd"
        },
        onSearch: function (event) {
            this.$('div[data-id="table-1"]').table('search', event);
        },
        onAdd: function (event) {
            var that = this;

            C1.loadView({
                viewOptions: {
                    data: this.options.data,
                    saveSuccess: function () {
                        that.onSearch();
                    }
                },
                url: this.$('div[data-id="table-1"]').data("add-modal-url")
            });
        },
        onModify: function (event) {
            var data = {
                ${(businessCode?uncap_first)!''}Id: this.$el.selectorVal('div[data-id="table-1"] input[name="${(businessCode?uncap_first)!''}Id"]:checked')
            };

            if (!data.${(businessCode?uncap_first)!''}Id) {
                return;
            }

            var that = this;

            C1.loadView({
                viewOptions: {
                    data: this.options.data,
                    saveSuccess: function () {
                        that.onSearch();
                    }
                },
                data: data,
                url: this.$('div[data-id="table-1"]').data("modify-modal-url")
            });
        },
        onDelete: function (event) {
            var data = {
                ${(businessCode?uncap_first)!''}Ids: this.$el.selectorsValJoinToString('div[data-id="table-1"] input[name="${(businessCode?uncap_first)!''}Id"]:checked')
            };

            if (!data.${(businessCode?uncap_first)!''}Ids) {
                return;
            }

            var that = this;

            C1.modal.confirm(this.options.data.i18n_confirm_delete, function () {
                $.ajax({
                    uiModal: C1.modal.blockUI(),
                    url: that.$('div[data-id="table-1"]').data("delete-api-url"),
                    data: data,
                    onSuccess: function (data, textStatus, jqXHR) {
                        C1.modal.alert(that.options.data.i18n_delete_success);

                        that.onSearch();
                    }
                });
            });
        },
        onSelectd: function (event) {
            var selectd = this.getOption('selectd');

            if (selectd && $.isFunction(selectd)) {
                selectd.call(this, event);
            }
        }
    });
});